package com.ddkj.netty.server;

import com.ddkj.netty.Setting;
import com.ddkj.netty.server.socket.airServer.TCPAirServerHandler;
import com.ddkj.netty.server.socket.chargingServer.ChargingServerHandler;
import com.ddkj.netty.server.websocket.basiswebsocket.Initializer;
import com.ddkj.netty.server.socket.basisServer.SocketBasisHandler;
import com.ddkj.netty.server.websocket.parkingwebsocket.ParkingInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.net.ssl.SSLException;

/**
 * server服务
 */
public class Server {

    private static final Log log = LogFactory.getLog(Server.class);

    //基础服务
    protected void tcpServer() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel channel) throws Exception {
                ChannelPipeline pipeline = channel.pipeline();
                pipeline.addLast("decoder", new StringDecoder());
                pipeline.addLast("encoder", new StringEncoder());
                pipeline.addLast(new SocketBasisHandler());
            }
        });
        bootstrap.bind(Setting.HOST, Setting.SERVER_PORT).sync();
        log.info(Setting.BASIS_SERVER_NAME + "已启动，端口-> " + Setting.SERVER_PORT);
    }

    //机场灯箱 AIR Server
    protected void tcpAirServer() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel channel) throws Exception {
                ChannelPipeline pipeline = channel.pipeline();
                pipeline.addLast("decoder", new StringDecoder());
                pipeline.addLast("encoder", new StringEncoder());
                pipeline.addLast(new TCPAirServerHandler());
            }
        });
        bootstrap.bind(Setting.SOCKET_AIR_SERVER_HOST, Setting.SOCKET_AIR_SERVER_PORT).sync();
        log.info(Setting.AIR_SERVER_NAME + "已启动，端口-> " + Setting.SOCKET_AIR_SERVER_PORT);
    }

    //充电桩服务
    protected void tcpChargingServer() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.childHandler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel channel) throws Exception {
                ChannelPipeline pipeline = channel.pipeline();
                pipeline.addLast(new ChargingServerHandler());
            }
        });
        bootstrap.bind(Setting.SOCKET_CHARGING_SERVER_HOST, Setting.SOCKET_CHARGING_SERVER_PORT).sync();
        log.info(Setting.CHARGING_SERVER_NAME + "已启动，端口-> " + Setting.SOCKET_CHARGING_SERVER_PORT);
    }


    //基础 WEB SOCKET 服务
    protected void webSocketServer() throws InterruptedException, SSLException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new Initializer());
        Channel ch = b.bind(Setting.WEB_SOCKET_HOST, Setting.WEB_SOCKET_PORT).sync().channel();
        log.info("WebSocket 【基础服务】 服务已启动，端口-> " + Setting.WEB_SOCKET_PORT);
        // ch.closeFuture().sync();
    }

    //WEB SOCKET PARKING 服务
    protected void webSocketParkingServer() throws InterruptedException, SSLException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ParkingInitializer());
        Channel ch = b.bind(Setting.WEB_SOCKET_PARKING_HOST, Setting.WEB_SOCKET_PARKING_PORT).sync().channel();
        log.info("WebSocket 【Parking】 服务已启动，端口-> " + Setting.WEB_SOCKET_PARKING_PORT);
        ch.closeFuture().sync();
    }

    //启动配置服务
    public static void main(String[] args) throws Exception {
        Server str = new Server();
      //  str.tcpServer();
        str.tcpAirServer();
       // str.tcpChargingServer();
        //str.webSocketServer();
        //str.webSocketParkingServer();
      //  str.tcpServer();
       // str.tcpAirServer();
        //str.tcpChargingServer();
       // str.webSocketServer();
       // str.webSocketParkingServer();
    }

}
